package it.trekker.dao;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class User implements Serializable {
	
	protected int id;
	protected String username;
	protected String passwd;
	protected String role;
	protected Set<Excursion> excursions;
	
	public User() {
		this(null);
	}
	
	public User(String username) {
		this.username = username;
		this.id = -1;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(unique = true)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	@OneToMany(
		fetch = FetchType.LAZY,
		cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE},
		mappedBy = "user"
	)
	public Set<Excursion> getExcursions() {
		return excursions;
	}

	public void setExcursions(Set<Excursion> excursions) {
		this.excursions = excursions;
	}
	
	@Override
	public String toString() {
		return String.format("username: %s, password: %s, role: %s", username, passwd, role);
	}
}
